<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2020 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use onekey\File;
use think\facade\View;
use think\exception\ValidateException;
use app\admin\BaseController;
use app\admin\model\AdminMenu as MenuModel;
use app\admin\validate\AdminMenu as AdminMenuValidate;
/**
 * 管理员菜单管理
 */
class AdminMenu extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $data = $this->request->menu;
            $keyword = input('post.keyword');
            if (! empty($keyword)) {
                $data = [];
                foreach ($this->request->menu as $key => $value) {
                    if (strstr($value['title'],$keyword) !== false || strstr($value['path'],$keyword) !== false) {
                        array_push($data, $value);
                    }
                }
            }
            $publicMenu = [];
            foreach ($this->request->menu as $key => $value) {
                if ($value['ifshow'] === 1) {
                    array_push($publicMenu, $value);
                }
            }
            return json(['status' => 'success', 'message' => '请求成功', 'data' => $data, 'publicMenu' => $publicMenu]);
        } else {
            $type   = plugin_list();
            array_unshift($type, ['title' => '系统', 'name' => '']);
            View::assign('type', $type);
            return View::fetch();
        }
    }
    
    
    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(AdminMenuValidate::class)->scene('save')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }  
            if (MenuModel::where('pid', $input['pid'])->where('path', $input['path'])->find()) {
                return json(['status' => 'error', 'message' => '此路径已存在，请勿重复添加']);   
            }
            MenuModel::create($input);
            return json(['status' => 'success', 'message' => '新增成功']);   
        }
    }

    /**
     * 保存更新的资源
     */
    public function update()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(AdminMenuValidate::class)->scene('update')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }  
            if (MenuModel::where('pid', $input['pid'])->where('id', '<>', $input['id'])->where('path', $input['path'])->find()) {
                return json(['status' => 'error', 'message' => '此路径已存在，请勿重复添加']);   
            }
            MenuModel::update($input);
            return json(['status' => 'success', 'message' => '修改成功']);
        }
    }

    /**
     * 删除指定资源
     */
    public function delete()
    {
        if ($this->request->isPost()) {
            MenuModel::recursiveDestroy(input('post.ids'));
            return json(['status' => 'success', 'message' => '删除成功']);
        }
    }
}
